package com.hahaliu.hahaliu.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hahaliu.hahaliu.mapper.FollowMapper;
import com.hahaliu.hahaliu.pojo.entity.Follow;
import com.hahaliu.hahaliu.pojo.vo.FollowVO;
import com.hahaliu.hahaliu.service.user.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Liubuzhu
* @description 针对表【follow】的数据库操作Service实现
* @createDate 2024-06-16 15:50:32
*/
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow>
    implements FollowService{
	@Autowired
	private FollowMapper followMapper;

	public FollowVO getFansCount(Integer uid) {
		// 查询关注数
		QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("follow_id", uid);
		Long followCount = followMapper.selectCount(queryWrapper);

		// 查询粉丝数
		queryWrapper.clear();
        queryWrapper.eq("followed_id", uid);
        Long fansCount = followMapper.selectCount(queryWrapper);

        FollowVO followVO = new FollowVO();
        followVO.setFansCount(Math.toIntExact(fansCount));
        followVO.setFollowCount(Math.toIntExact(followCount));

        return followVO;
	}

	public Boolean isFollow(Integer uid, Integer followUid) {
		QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follow_id", uid);
        queryWrapper.eq("followed_id", followUid);
        Long count = followMapper.selectCount(queryWrapper);
        return count > 0;
	}

	public void follow(Integer uid, Integer followUid) {
		Follow follow = new Follow();
        follow.setFollowId(uid);
        follow.setFollowedId(followUid);
        followMapper.insert(follow);
	}
}




